using System;
using System.Xml.Linq;

namespace Common
{
    public class Creator
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static string CreateXml(Command command)
        {
            try
            {
                int count = command.Commands.Count;
                XElement[] cmds = new XElement[count];
                for (int i = 0; i < count; i++)
                {
                    cmds[i] = new XElement("command",
                        new XAttribute("name", command.Commands[i].Name),
                        new XAttribute("value", command.Commands[i].Value)
                        );
                }

                var doc = new XDocument(
                        new XDeclaration("1.0", "utf-8", "yes"),
                        new XElement("root",
                              new XElement("type", command.Type),
                              new XElement("operation", command.Operation),
                              new XElement("commands", cmds)
                              )
                    );

                return doc.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}

